/*
 * Copyright (C) 2004, Velocityme Partnership
 * UserActions.java
 *
 * Created on 25 September 2004, 04:20
 */

package com.velocityme.www.interfaces.nodeactions;

import com.velocityme.interfaces.*;
import com.velocityme.utility.DuplicateUserException;
import com.velocityme.utility.PasswordIncorrectException;
import com.velocityme.utility.PermissionDeniedException;
import com.velocityme.valueobjects.NodeValue;
import com.velocityme.valueobjects.PersonValue;
import com.velocityme.valueobjects.UserValue;
import com.velocityme.www.actionforms.ChangePasswordActionForm;
import com.velocityme.www.actionforms.nodeactionforms.UserValueActionForm;
import com.velocityme.www.interfaces.ShowNodeAction;
import com.velocityme.www.utility.NodeUtil;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.struts.actions.DispatchAction;

/**
 *
 * @author  Robert
 */
public class UserActions extends NodeActions {
    
    /** Creates a new instance of UserActions */
    public UserActions() {
    }
    
    public ActionForward show(ActionMapping mapping, 
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        NodePK nodePK = (NodePK)request.getAttribute("nodePK");
        UserPK userPK = new UserPK(nodePK.getNodeId());
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        session.setAttribute("userValue", cs.getUserValue(key, userPK));
        session.setAttribute("groupValues", cs.getUserGroupValues(key, userPK));
        return mapping.findForward("ACTION_DISPLAY");
    }
    
    public ActionForward showCreate(ActionMapping mapping, 
                                    ActionForm form,
                                    HttpServletRequest request,
                                    HttpServletResponse response) throws Exception {
        NodePK nodePK = (NodePK)request.getAttribute("nodePK");
        UserPK userPK = new UserPK(nodePK.getNodeId());
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        session.setAttribute("parentNodePK", nodePK);
        session.setAttribute("personPairs", NodeUtil.getAllPersonPairs(key, cs));
        UserValueActionForm userValueActionForm = new UserValueActionForm();
        session.setAttribute("userActionForm", userValueActionForm);
        UserValue userValue = new UserValue();
        userValue.setNodeValue(new NodeValue());
        session.setAttribute("userValue", userValue);
        return mapping.findForward("ACTION_EDIT");
    }
    
    public ActionForward showEdit(ActionMapping mapping, 
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response) throws Exception {
        NodePK nodePK = (NodePK)request.getAttribute("nodePK");
        UserPK userPK = new UserPK(nodePK.getNodeId());
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        UserValue userValue = cs.getUserValue(key, userPK);
        session.setAttribute("userValue", userValue);
        UserValueActionForm userValueActionForm = new UserValueActionForm();
        userValueActionForm.setName(userValue.getNodeValue().getName());
        userValueActionForm.setDescription(userValue.getNodeValue().getDescription());
        userValueActionForm.setPersonId(userValue.getPersonValue().getPersonId());
        session.setAttribute("userActionForm", userValueActionForm);
        session.setAttribute("personPairs", NodeUtil.getAllPersonPairs(key, cs));
        return mapping.findForward("ACTION_EDIT");
    }
    
    public ActionForward edit(ActionMapping mapping, 
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        NodePK nodePK;
        if (request.getParameter("btnSubmit").equals("OK")) {
            UserValue userValue = (UserValue)session.getAttribute("userValue");
            userValue.setUserName(((UserValueActionForm)form).getName());
            userValue.getNodeValue().setDescription(((UserValueActionForm)form).getDescription());
            PersonValue personValue = cs.getPersonValue(key, new PersonPK(((UserValueActionForm)form).getPersonId()));
            userValue.setPersonValue(personValue);
            UserPK userPK;
            if (userValue.userIdHasBeenSet()) {
                userPK = userValue.getPrimaryKey();
                cs.userEdit(key, userPK, userValue);
            }
            else {
                ActionErrors errors = new ActionErrors();
                String password = ((UserValueActionForm)form).getPassword();
                String repeatPassword = ((UserValueActionForm)form).getRepeatPassword();
                if (password.equals(repeatPassword)) {
                    try {
                        NodePK parentNodePK = (NodePK)session.getAttribute("parentNodePK");
                        userPK = cs.userCreate(key, parentNodePK, userValue, password);
                    }
                    catch(DuplicateUserException e) {
                        errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.username.exists"));
                        saveErrors(request, errors);
                        return mapping.findForward("ACTION_RETRY");
                    }
                }
                else {
                    errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.both.passwords.equal"));
                    saveErrors(request, errors);
                    return mapping.findForward("ACTION_RETRY");
                }
            }
            nodePK = new NodePK(userPK.getUserId());
        }
        // Cancel simply displays the original node again
        else {
            nodePK = new NodePK((Integer)session.getAttribute("nodeId"));
        }
        request.setAttribute("nodePK", nodePK);
        return mapping.findForward("ACTION_SUCCESS");
    }
    
    public ActionForward changePassword(ActionMapping mapping, 
                                        ActionForm form,
                                        HttpServletRequest request,
                                        HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        NodePK nodePK = new NodePK((Integer)session.getAttribute("nodeId"));
        UserPK userPK = new UserPK((Integer)session.getAttribute("nodeId"));
        if (request.getParameter("btnSubmit").equals("OK")) {
            String oldPassword = ((ChangePasswordActionForm)form).getOldPassword();
            String newPassword = ((ChangePasswordActionForm)form).getNewPassword1();
            String repeatPassword = ((ChangePasswordActionForm)form).getNewPassword2();
            ActionErrors errors = new ActionErrors();
            if (newPassword.equals(repeatPassword)) {
                try {
                    cs.userChangePassword(key, userPK, oldPassword, newPassword);
                }
                catch(PermissionDeniedException e) {
                    errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.permission.denied"));
                }
                catch(PasswordIncorrectException e) {
                    errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.old.password.not.match"));
                }
            }
            else {
                errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.both.passwords.equal"));
            }
            if (!errors.empty()) {
                saveErrors(request, errors);
                return mapping.findForward("ACTION_RETRY");
            }
        }
        // Cancel simply displays the original user again
        request.setAttribute("nodePK", nodePK);
        return mapping.findForward("ACTION_SUCCESS");
    }
    
    public ActionForward enable(ActionMapping mapping, 
                                ActionForm form,
                                HttpServletRequest request,
                                HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        NodePK nodePK = new NodePK((Integer)session.getAttribute("nodeId"));
        UserPK userPK = new UserPK((Integer)session.getAttribute("nodeId"));
        UserValue userValue = (UserValue)session.getAttribute("userValue");
        if (request.getParameter("btnSubmit").equals("OK")) {
            Boolean isEnabled = new Boolean(!userValue.getIsEnabled().booleanValue());
            ActionErrors errors = new ActionErrors();
            try {
                cs.userSetEnabled(key, userPK, isEnabled);
            }
            catch(PermissionDeniedException e) {
                errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.permission.denied"));
            }
            if (!errors.empty()) {
                saveErrors(request, errors);
                return mapping.findForward("ACTION_FAILED");
            }
        }
        // Cancel simply displays the original user again
        request.setAttribute("nodePK", nodePK);
        return mapping.findForward("ACTION_SUCCESS");
    }
    
    public ActionForward showLogins(ActionMapping mapping, 
                                    ActionForm form,
                                    HttpServletRequest request,
                                    HttpServletResponse response) throws Exception {
        NodePK nodePK = (NodePK)request.getAttribute("nodePK");
        UserPK userPK = new UserPK(nodePK.getNodeId());
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        NodeValue nodeValue = cs.getNodeValue(key, nodePK);
        ShowNodeAction.setupTreeDisplay(session, nodeValue, "Logins");
        session.setAttribute("userValue", cs.getUserValue(key, userPK));
        session.setAttribute("loginValues", cs.getUserLoginValues(key, userPK));
        return mapping.findForward("ACTION_DISPLAY");
    }
    
    public ActionForward addBookmarkNode(ActionMapping mapping, 
                                    ActionForm form,
                                    HttpServletRequest request,
                                    HttpServletResponse response) throws Exception {
        NodePK nodePK = (NodePK)request.getAttribute("nodePK");
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        cs.addBookmarkNode(key, nodePK);
        return mapping.findForward("ACTION_SUCCESS");                                        
    }
    
    public ActionForward removeBookmarkNode(ActionMapping mapping, 
                                            ActionForm form,
                                            HttpServletRequest request,
                                            HttpServletResponse response) throws Exception {
        NodePK nodePK = (NodePK)request.getAttribute("nodePK");
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        cs.removeBookmarkNode(key, nodePK);
        return mapping.findForward("ACTION_SUCCESS");                                        
    }
    
}
